//
//  UIView+Ext.swift
//
//
//  Created by gongkai on 15/3/25.
//  Copyright (c) 2015年 gongkai. All rights reserved.
//

import Foundation
import UIKit

extension UIView{
    
    // MARK:位置相关
    var left:CGFloat{
        get{
            return self.frame.origin.x
        }
        set(_left){
            self.frame.origin.x = _left
        }
    }
    
    var top:CGFloat{
        get{
            return self.frame.origin.y
        }
        set(_top){
            self.frame.origin.y = _top
        }
    }
    
    var width:CGFloat{
        get{
            return self.frame.size.width
        }
        set(_width){
            self.frame.size.width = _width
        }
    }
    
    var height:CGFloat{
        get{
            return self.frame.size.height
        }
        set(_height){
            self.frame.size.height = _height
        }
    }
    
    var right:CGFloat{
        get{
            return self.width+self.left
        }
        set(_right){
            self.left = _right-self.width
        }
    }
    
    var bottom:CGFloat{
        get{
            return self.height+self.top
        }
        set(_bottom){
            self.top = _bottom-self.height
        }
    }
    
    //  MARK:对齐相关
    
    /**
    * 根据传入的子视图计算出中心开始点
    **/
    func centerWithSubview(subview:UIView)->CGPoint{
        return CGPointMake(self.centerHorizontalWithSubview(subview), self.centerVerticalWithSubview(subview))
    }
    
    /**
    *  根据传入的子视图与当前视图计算出水平中心开始点
    */
    func centerHorizontalWithSubview(subview:UIView)->CGFloat{
        return self.width/2 - subview.width/2
    }
    
    /**
    *  根据传入的子视图与当前视图计算出垂直中心开始点
    */
    func centerVerticalWithSubview(subview:UIView)->CGFloat{
        return self.height/2 - subview.height/2
    }
    
    /**
    *  居中增加子视图
    */
    func addSubviewToCenter(subview:UIView){
        subview.frame.origin = self.centerWithSubview(subview)
        self .addSubview(subview)
    }
    
    func addSubviewToHorizontalCenter(subview:UIView){
        subview.left = self.centerHorizontalWithSubview(subview)
        self .addSubview(subview)
    }
    
    func addSubviewToVerticalCenter(subview:UIView){
        subview.top = self.centerVerticalWithSubview(subview)
        self .addSubview(subview)
    }
    
    /**
    边缘对齐增加视图
    **/
    func addSubviewToLeftAlign(subview:UIView){
        subview.left = 0
        self.addSubview(subview)
    }
    
    func addSubviewToRightAlign(subview:UIView){
        subview.left = self.width - subview.width
        self.addSubview(subview)
    }
    
    func addSubviewToTopAlign(subview:UIView){
        subview.top = 0
        self.addSubview(subview)
    }
    
    func addSubviewToBottomAlign(subview:UIView){
        subview.top = self.height - subview.height
        self.addSubview(subview)
    }
    
    
    //MARK:视图层次相关
    
    
    //将视图置于父视图最上面
    func orderIndex()->Int{
        let subviews = self.superview?.subviews as! [UIView]
        for (index,subview) in enumerate(subviews){
            if subview===self {
                return index
            }
        }
        return 0
    }
    
    func bringToFront(){
        self.superview?.bringSubviewToFront(self)
    }
    
    /**
    *  将视图置于父视图最下面
    */
    func sendToBack(){
        self.superview?.sendSubviewToBack(self)
    }
    
    /**
    *  视图层次上移一层
    */
    func bringOneLevelUp(){
        let currentIndex = self.orderIndex()
        self.superview?.exchangeSubviewAtIndex(currentIndex, withSubviewAtIndex: currentIndex+1)
    }
    
    /**
    *  视图层次下移一层
    */
    func sendOneLevelDown(){
        let currentIndex = self.orderIndex()
        self.superview?.exchangeSubviewAtIndex(currentIndex, withSubviewAtIndex: currentIndex-2)
    }
    
    
    /**
    *  是否在最上面
    */
    func isInFront()->Bool{
        let subviews = self.superview?.subviews as! [UIView]
        return subviews.last! === self
    }
    
    /**
    *  是否在最下面
    */
    func isAtBack()->Bool{
        let subviews = self.superview?.subviews as! [UIView]
        return subviews[0] === self
    }
    
    /**
    *  交换层次
    */
    func swapDepthsWithView(swapView:UIView){
        self.superview?.exchangeSubviewAtIndex(self.orderIndex(), withSubviewAtIndex: swapView.orderIndex())
    }
    
    /**
    *  清空所有子视图
    */
    func removeAllSubview(){
        for view in self.subviews{
            view.removeFromSuperview()
        }
    }
    
}